<%@page import="com.mongodb.DBObject"%>
<%@page import="com.mongodb.DBCursor"%>
<%
String[] listError = (String[]) request.getAttribute("listError");
String type = (String) request.getAttribute("type");
String success = (String) request.getAttribute("success");
String blogurl = (String) request.getAttribute("blogurl");
String usernameblog = (String) request.getAttribute("usernameblog");
String email = (String) request.getAttribute("email");
String blogid = (String) request.getAttribute("blogid");
DBCursor cursor = (DBCursor)request.getAttribute("cursor");
DBObject objects = (DBObject) request.getAttribute("object");
%>
<jsp:include page="layout/header.jsp">
	<jsp:param name="title" value="Edit blog" />
	<jsp:param name="description" value="" />
	<jsp:param name="keywords" value="" />
	<jsp:param name="page" value="home" />
</jsp:include>	
	<div id="content">
        	<div id="content_inner">
            	<div id="maincontent">
                    <div id="userform" class="clearafter">
                        <div class="signupform">
                        	<%
                        	if(success==null)
                        	{
                        	%>
                            <h2 class="title">Edit an auto blog</h2>
                            
                            <form method="post" action="" name="signupform">
                                <div class="blog-type clearafter">
                                    <p>
                                    	<span class="tooltips">
	                                    	<img src="images/help1.png">
                                    		Choose the type of your blog
                                    	
	                                        <span>
	                                            Choose BlogSpot if your autoblog is BlogSpot and Choose Wordpress if Your AutoBlog is Wordpress. 
	                                        </span>	
	                                    </span>    
                                    </p>
                                    <div <%=objects!=null&&objects.get("type").toString().equals("wordpress")||objects==null?"style=\"background:#CCC\"":"" %> class='typelogo'>
                                        <img src="/images/logo-wordpress.png" alt="wordpress logo" title="wordpress logo" />
                                        <label>                                    
                                            <input type="radio" name="type" value="wordpress"  class="type" <%=objects!=null&&objects.get("type").toString().equals("wordpress")?"checked=\"checked\"":"" %> <%=objects==null?"checked=\"checked\"":"" %>/>
                                            Wordpress
                                        </label>
                                    </div>
                                    <div <%=objects!=null&&objects.get("type").toString().equals("blogger")?"style=\"background:#CCC\"":"" %> class='typelogo'>
                                        <img src="/images/logo-blogspot-orange.png" alt="blogspot logo" title="blogspot logo" />
                                        <label>                                    
                                            <input type="radio" name="type" value="blogger" class="type" <%=objects!=null&&objects.get("type").toString().equals("blogger")?"checked=\"checked\"":"" %>/>
                                            Blogger
                                        </label>
                                    </div>
                                </div>
                                <div id='load-field'>
				
								</div>
								<div class='check-info' id="test" style="background: none repeat scroll 0 0 #FFFFFF;border: 1px solid #E5E5E5;border-radius: 3px 3px 3px 3px;padding: 10px;margin-bottom: 10px;width: 94%">
									<a href="javascript:void()" style="text-decoration: none;">Check information &nbsp;</a>
									<div id="loadder" style="display: inline;"></div>
								</div>
                                <p>
                                    <input type="submit" value="Edit" disabled="disabled" style="background: #CCC;border: 1px solid #CCC;" id='submit'/>
                                </p>
                            </form>
                            <div id='wordpress' style="display: none;">
                             <p>
                                 <label>
                                 	<span class="tooltips">
	                                  	<img src="images/help1.png">
	                                      Blog's url
	                                      <br />
	                                      <input class="input" type="text" name="blogurl" id='blogurl' value="<%=objects!=null?objects.get("blogurl").toString():""%>"/>
	                                      <span>
	                                          Click to the tilte link to redirect details page that you chose Newest Article. Then, input Selector format tag to get Title.
	                                      </span>
	                                      <%=listError[0]!=null?"<div class=\"error\">"+listError[0]+"</div>":"" %>
                                     </span>
                                 </label>
                             </p>
                             <p>
                                 <label>
                                 	<span class="tooltips">
	                                  	<img src="images/help1.png">
	                                      Blog's username
	                                      <br />
	                                      <input class="input" type="text" name="usernameblog" id='usernameblog' value="<%=objects!=null&&objects.get("usernameblog")!=null?objects.get("usernameblog").toString():""%>"/>
	                                      <span>
	                                          Input your username that you registed at Wordpress. 
	                                      </span>
	                                     <%=listError[1]!=null?"<div class=\"error\">"+listError[1]+"</div>":"" %> 
                                     </span>
                                 </label>
                             </p>
                             <p>
                                 <label>
                                 	<span class="tooltips">
	                                  	<img src="images/help1.png">
	                                      Blog's password
	                                      <br />
	                                      <input class="input" type="password" name="passwordblog" id='passwordblog' value="<%=objects!=null&&objects.get("passwordblog")!=null&&objects.get("passwordblog")!=null?objects.get("passwordblog").toString():""%>"/>
	                                      <span>
	                                          Input your password that you registed at Wordpress.
	                                      </span>
	                                     <%=listError[2]!=null?"<div class=\"error\">"+listError[2]+"</div>":"" %> 
                                     </span>
                                 </label>
                             </p>
                            </div>
                            <div id='blogger' style="display: none;">
                            <p>
                                 <label>
                                 	<span class="tooltips">
	                                  	<img src="images/help1.png">
	                                      Blogger url
	                                      <br />
	                                      <input class="input" type="text" name="blogurl" value="<%=objects!=null?objects.get("blogurl").toString():""%>" id="blogurlblogger"/>
	                                      <span>
	                                          Url blog
	                                      </span>
	                                     <%=listError[3]!=null?"<div class=\"error\">"+listError[3]+"</div>":"" %>  
                                     </span>
                                 </label>
                             </p>
                             <p>
                                 <label>
                                 	<span class="tooltips">
	                                  	<img src="images/help1.png">
	                                      Blogger ID
	                                      <br />
	                                      <input class="input" type="text" name="blogid" value="<%=objects!=null&&objects.get("blogid")!=null?objects.get("blogid").toString():""%>" id='blogid'/>
	                                      <span>
	                                          Input BloggerId at your Blogger
	                                      </span>
	                                     <%=listError[4]!=null?"<div class=\"error\">"+listError[4]+"</div>":"" %>  
                                     </span>
                                 </label>
                             </p>
                             <p>
                                 <label>
                                 	<span class="tooltips">
	                                  	<img src="images/help1.png">
	                                      Email
	                                      <br />
	                                      <input class="input" type="text" name="email" value="<%=objects!=null&&objects.get("email")!=null?objects.get("email").toString():""%>" id='email'/>
	                                      <span>
	                                          Input your username that you registed at Blogger.  
	                                      </span>
	                                      <%=listError[5]!=null?"<div class=\"error\">"+listError[5]+"</div>":"" %> 
                                     </span>
                                 </label>
                             </p>
                             <p>
                                 <label>
                                 	<span class="tooltips">
	                                  	<img src="images/help1.png">
	                                      Password
	                                      <br />
	                                      <input class="input" type="password" name="passwordemail" id='passwordemail' value="<%=objects!=null&&objects.get("passwordemail")!=null?objects.get("passwordemail").toString():""%>"/>
	                                      <span>
	                                          Input your password that you registed at Blogger. 
	                                      </span>
	                                      <%=listError[6]!=null?"<div class=\"error\">"+listError[6]+"</div>":"" %> 
                                     </span>
                                 </label>
                             </p>
                            </div>
                            <%
                        	} else {
                        	%>
                        		<h2 class="title">Edit auto blog successful</h2>
                        		<div class="success">
                        			<p>Edit auto blog successful</p>
                                	<p>Click <a href="/manager-blog">here</a> to return to the blog management</p>
                        		</div>
                        	<%
                        	}
                            %>
                        </div>
                        
                        <div class="domainlisting">
                            <%
                            if(cursor.size()>0)
                            {
                            %>
                            	<h2 class="title">List auto blog</h2>
	                            <ul>
	                            	<%
	                            	while(cursor.hasNext())
	                            	{
	                            		DBObject object = cursor.next();
	                            	%>
	                            		<li><a class="icon-<%=object.get("type").toString() %>" href="/work?blog=<%=object.get("blogurl") %>"><%=object.get("blogurl") %></a></li>
	                                <%
	                            	}
	                                %>
	                            </ul>
                            <%
                            } else {
                            %>
                            	<h2 class="title">Instruction</h2>
                            	<div>
                            		Connect to mongo server on local, if u install mongo on other server, u can connect to mongo server by replace "localhost" = "server name". Interesting of Mongo are : u can put your source code and database on 2 others server -> help to use and sharing a database, very convenient
                            	</div>
                            <%
                            }
                            %>
                        </div>
                    </div>
                </div>
            </div>
        </div>
<script type="text/javascript">
	$(document).ready(function(){
		var type = '<%=(objects!=null&&objects.get("type")!=null&&objects.get("type").toString().equals("blogger"))?"blogger":"wordpress"%>';
		$(".typelogo").css("cursor","pointer");
		$("#load-field").html($("#"+type).html());
		$(".typelogo").click(function(){
			$("#loadder").html("");
			if($(this).find(".type").val()=="wordpress")
			{
				$("#load-field").html($("#wordpress").html());		
			}
			if($(this).find(".type").val()=="blogger")
			{
				$("#load-field").html($("#blogger").html());		
			}
		});
		$(".typelogo").click(function(){
			$(".typelogo").css("background","#FFF");
			$(this).css("background","#CCC");
			$(".type").attr("checked",false);
			$(this).find(".type").attr("checked","checked");
		});
		$("#test a").click(function(){
			$("#loadder").html("<img src='/images/ajax-loader.gif'/>");
			if($(".type:checked").val()=="wordpress")
			{
				$.get(
						'/test',
						{
							type:$(".type:checked").val(),
							blogurl:$("#blogurl").val(),
							usernameblog:$("#usernameblog").val(),
							passwordblog:$("#passwordblog").val(),
						},
						function(data)
						{
							if(data==1)
							{
								var xml_check = "<span class=\"icon-large-checkmark\">Success</span>";
								xml_check += "<div>We sent a testing post to your website to ensure that your information is correct.</div>";
								xml_check += "<div>Please go to your website, find the post named : \"Testing post from hatforent\" and remove it.</div>";
								$("#loadder").html(xml_check);	
								$("#submit").attr("disabled",false);
								$("#submit").css("background","#21759B");
								$("#submit").css("border","1px solid #21759B");
							}else{
								var xml_check = "<span class=\"icon-large-close\">Wrong information.</span>";
								xml_check+="<div>we sent a testing post to your website but not successful. Please recheck your information.</div>";
								xml_check+="<div>If your information is correct, please make sure that you have active <a href=\"/enable-xml-rpc\" target='_blank'>XML-RPC</a></div>";
								xml_check+="<div>Note : The username that you provide must have permission to post content.</div>";
								$("#loadder").html(xml_check);
							}
							
						}
				);
			}
			if($(".type:checked").val()=="blogger")
			{
				$.get(
						'/test',
						{
							type:$(".type:checked").val(),
							blogurlblogger:$("#blogurlblogger").val(),
							email:$("#email").val(),
							passwordemail:$("#passwordemail").val(),
							blogid:$("#blogid").val(),
						},
						function(data)
						{
							if(data==1)
							{
								var xml_check = "<span class=\"icon-large-checkmark\">Success</span>";
								xml_check += "<div>We sent a testing post to your website to ensure that your information is correct.</div>";
								xml_check += "<div>Please go to your website, find the post named : \"Testing post from hatforent\" and remove it.</div>";
								$("#loadder").html(xml_check);	
								$("#submit").attr("disabled",false);
								$("#submit").css("background","#21759B");
								$("#submit").css("border","1px solid #21759B");
							}else{
								var xml_check = "<span class=\"icon-large-close\">Wrong information.</span>";
								xml_check+="<div>we sent a testing post to your website but not successful. Please recheck your information.</div>";
								xml_check+="<div>Note : The email that you provide must have permission to post content.</div>";
								$("#loadder").html(xml_check);
							}
							
						}
				);
			}
		});
	});
</script>        
<%@ include file='/layout/footer.jsp'%>        